package com.have.blog.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.PictureCategoryDto;
import com.have.blog.admin.service.AdminPictureSortService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.PictureCategoryMapper;
import com.have.blog.model.mapper.PictureMapper;
import com.have.blog.model.models.PictureCategory;
import com.have.blog.model.models.PictureCategoryExample;
import com.have.blog.model.models.PictureExample;
import com.have.blog.picture.service.PictureFeignApi;
import com.have.blog.utils.MyBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
@Slf4j
public class AdminPictureSortServiceImpl implements AdminPictureSortService {
    @Autowired
    private PictureCategoryMapper pictureCategoryMapper;
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private PictureFeignApi pictureFeignApi;

    @Override
    public List<PictureCategoryDto> getList(PictureCategoryDto pictureSortDto) {
        PageHelper.startPage(pictureSortDto.getPageNum(), pictureSortDto.getPageSize());
        PictureCategoryExample example = new PictureCategoryExample();
        PictureCategoryExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(pictureSortDto.getKeyword()) && !StringUtils.isEmpty(pictureSortDto.getKeyword().trim())) {
            criteria.andNameLike("%" + pictureSortDto.getKeyword() + "%");
        }
        if (pictureSortDto.getIsShow() != null) {
            criteria.andIsShowEqualTo(pictureSortDto.getIsShow());
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("sort desc");
        List<PictureCategory> list = pictureCategoryMapper.selectByExample(example);
        List<PictureCategoryDto> resultList = MyBeanUtils.copyListProperties(list, PictureCategoryDto::new);

        List<Integer> picUids = new ArrayList<>();
        resultList.forEach(item -> {
            if (item.getCoverPictureUid() != null) {
                picUids.add(item.getCoverPictureUid());
            }
        });

        if (!CollectionUtils.isEmpty(picUids)) {
            Wrapper<Map<Integer, String>> pictureWrapper = pictureFeignApi.getPicturesUrl(picUids);
            if (pictureWrapper.isSuccess()) {
                Map<Integer, String> pictureMap = pictureWrapper.getData();
                for (PictureCategoryDto dto : resultList) {
                    dto.setPhotoUrl(pictureMap.get(dto.getCoverPictureUid()));
                }
            }
        }
        return resultList;
    }

    @Override
    public Wrapper addPictureSort(PictureCategoryDto pictureSortDto) {
        PictureCategoryExample example = new PictureCategoryExample();
        PictureCategoryExample.Criteria criteria = example.createCriteria()
                .andNameEqualTo(pictureSortDto.getName());
        if (pictureSortDto.getParentUid() != null) {
            criteria.andParentUidEqualTo(pictureSortDto.getParentUid());
        }
        List<PictureCategory> pictureSortList = pictureCategoryMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(pictureSortList)) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_EXIST);
        }
        PictureCategory PictureCategory = new PictureCategory();
        MyBeanUtils.copyProperties(pictureSortDto, PictureCategory);
        int insert = pictureCategoryMapper.insertSelective(PictureCategory);
        if (insert > 0) {
            return Wrapper.success(AdminMessageConstant.INSERT_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.INSERT_FAIL);
    }

    public Wrapper editPictureCategory(PictureCategoryDto pictureSortDto) {
        PictureCategory pictureCategory = pictureCategoryMapper.selectByPrimaryKey(pictureSortDto.getUid());
        if (pictureCategory == null) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_NOT_EXIST);
        }
        pictureCategory.setName(pictureSortDto.getName());
        pictureCategory.setParentUid(pictureSortDto.getParentUid());
        pictureCategory.setSort(pictureSortDto.getSort());
        pictureCategory.setCoverPictureUid(pictureSortDto.getCoverPictureUid());
        pictureCategory.setIsShow(pictureSortDto.getIsShow());
        int update = pictureCategoryMapper.updateByPrimaryKeySelective(pictureCategory);
        if (update > 0) {
            return Wrapper.success(AdminMessageConstant.UPDATE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
    }

    @Override
    public Wrapper deletePictureCategory(PictureCategoryDto pictureSortDto) {
        PictureExample pictureExample = new PictureExample();
        pictureExample.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andPictureCategoryUidEqualTo(pictureSortDto.getUid());
        long count = pictureMapper.countByExample(pictureExample);
        if (count > 0) {
            return Wrapper.failed(AdminMessageConstant.PICTURE_UNDER_THIS_SORT);
        }
        PictureCategory pictureSort = pictureCategoryMapper.selectByPrimaryKey(pictureSortDto.getUid());
        pictureSort.setStatus(BlogStatus.DISABLE.getStatus());
        int delete = pictureCategoryMapper.updateByPrimaryKeySelective(pictureSort);
        if (delete > 0) {
            return Wrapper.success(AdminMessageConstant.DELETE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
    }

    @Override
    public Wrapper stickPictureCategory(PictureCategoryDto pictureSortDto) {
        PictureCategory pictureSort = pictureCategoryMapper.selectByPrimaryKey(pictureSortDto.getUid());
        PageHelper.startPage(1, 1);
        PictureCategoryExample example = new PictureCategoryExample();
        example.setOrderByClause("sort desc");
        List<PictureCategory> pictureCategoryList = pictureCategoryMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(pictureCategoryList) || pictureCategoryList.get(0).getUid().equals(pictureSortDto.getUid())) {
            return Wrapper.successWithMessage(AdminMessageConstant.OPERATION_SUCCESS);
        }
        PictureCategory maxSort = pictureCategoryList.get(0);
        pictureSort.setSort(maxSort.getSort() + 1);
        pictureSort.setUpdateTime(new Date());
        int update = pictureCategoryMapper.updateByPrimaryKeySelective(pictureSort);
        if (update > 0) {
            return Wrapper.success(AdminMessageConstant.OPERATION_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.OPERATION_FAIL);
    }

    @Override
    public PictureCategoryDto getById(Integer uid) {
        PictureCategory pictureSort = pictureCategoryMapper.selectByPrimaryKey(uid);
        if (pictureSort == null) {
            return null;
        }
        PictureCategoryDto pictureSortDto = new PictureCategoryDto();
        MyBeanUtils.copyProperties(pictureSort, pictureSortDto);
        return pictureSortDto;
    }
}
